package cn.zzf.core;

import cn.zzf.mapper.ClassInfoMapper;
import cn.zzf.mapper.TeacherInfoMapper;
import cn.zzf.mapper.TeacherLinkMapper;
import cn.zzf.po.ClassInfoExample;
import cn.zzf.po.TeacherInfo;
import cn.zzf.po.TeacherInfoExample;
import cn.zzf.po.TeacherLink;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.Date;
import java.util.List;

/**
 * @author GaoFeng2017
 * @date 2018-02-20 12:14:52
 **/

public class LinkWriteToDb {
    private List<LinkItem> linkItems;

    public LinkWriteToDb(List<LinkItem> linkItems) {
        this.linkItems = linkItems;
    }

    public void writeAll() throws IOException {
        if (!linkItems.isEmpty()) {
            SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(Resources.getResourceAsStream("mybatis.xml"));
            SqlSession sqlSession = sessionFactory.openSession();
            TeacherLinkMapper teacherLinkMapper = sqlSession.getMapper(TeacherLinkMapper.class);
            TeacherInfoMapper teacherInfoMapper = sqlSession.getMapper(TeacherInfoMapper.class);
            ClassInfoMapper classInfoMapper = sqlSession.getMapper(ClassInfoMapper.class);
            for (LinkItem linkItem : linkItems) {
                TeacherLink teacherLink = new TeacherLink();
                teacherLink.setCategory(linkItem.getCategory());
                TeacherInfoExample teacherInfoExample = new TeacherInfoExample();
                TeacherInfoExample.Criteria criteria = teacherInfoExample.createCriteria();
                criteria.andNameEqualTo(linkItem.getTeacher());
                System.out.println(linkItem.getTeacher());
                teacherLink.setTeacherId(teacherInfoMapper.selectByExample(teacherInfoExample).get(0).getId());

                ClassInfoExample classInfoExample = new ClassInfoExample();
                ClassInfoExample.Criteria criteria1 = classInfoExample.createCriteria();
                criteria1.andNameEqualTo(linkItem.getClassInfo());
                teacherLink.setClassId(classInfoMapper.selectByExample(classInfoExample).get(0).getId());

                teacherLink.setCredit(linkItem.getCredit());
                teacherLink.setGmtCreate(new Date());
                teacherLink.setName(linkItem.getName());
                teacherLink.setTeams(Short.valueOf(linkItem.getTeems() + ""));
                teacherLink.setTotalPeople(Short.valueOf(linkItem.getTotal_people() + ""));
                String[] strs = linkItem.getWeek().split(",");
                teacherLink.setWeeks(Byte.valueOf(linkItem.getWeeks() + ""));

                for (String s : strs) {
                    teacherLink.setWeek(Byte.parseByte(s));
                    teacherLinkMapper.insert(teacherLink);
                    teacherLink.setId(null);
                }
            }

            sqlSession.commit();
            sqlSession.close();
        }
    }
}
